<body>
    <h2>我的商业机会</h2>
    <div style="margin:10px 0;"></div>
    <table id="list"></table>
    <div id="dlg" class="easyui-dialog" style="width:600px;height:500px;padding:10px 20px"
            closed="true" buttons="#dlg-buttons">
        <div class="ftitle">增加商业机会</div>
        <form id="fm" method="post" novalidate>
       	 	<input name="id" type="hidden">
       	 	<div class="fitem">
                <label>商业机会名称:</label>
                <input name="name" class="easyui-validatebox" required="true">
            </div>
            <div class="fitem">
                <label>客户名字:</label>
                <span id="customerName" style="display:inline-block;width:150px;padding:3px"></span>
                <a href="#" onclick="javascript:opportunityListJs.opendiag('component/customer/addCustomer.html',this)">增加客户</a>
                <a href="#" onclick="javascript:opportunityListJs.opendiag('component/customer/findCustomer.html',this)">查找</a>
                <input name="customer" type="hidden">
            </div>
            <div class="fitem">
                <label>来源:</label>
                <input name="opp_from" id="opp_from">
            </div>
            <div class="fitem">
                <label>当前阶段:</label>
                
               	<select class="easyui-combobox" name="strategy">
                	<option value="processing">进行中</option>
                	<option value="success">成功</option>
                	<option value="failed">失败</option>
                </select>
            </div>
           	<div class="fitem">
                <label>相关整形项目:</label>
                <input id="items">
            </div>
            <div class="fitem" id="owner_div">
                <label>所有人:</label>
                <input name="owner">
            </div>
        </form>
    </div>

    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="opportunityListJs.save()">保存</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
    </div>

    <div id="CustomerAction" title="添加客户" class="easyui-dialog" style="width:600px;height:500px;padding:10px 20px;"
            closed="true">
    </div>
    <div id="CustomerDetail" title="客户详细信息" class="easyui-dialog" closed="true" style="width:850px;height:550px">
    </div>
    
    <div id="tb" style="padding:5px;height:auto">
        <div style="margin-bottom:5px;vertical-align:middle">
            <a href="#" onclick="javascript:opportunityListJs.newCA()" class="easyui-linkbutton" iconCls="icon-add" plain="true">增加</a>
            <a href="#" onclick="javascript:opportunityListJs.edit()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">修改</a>
            <a href="#" class="easyui-linkbutton"  plain="true">过滤条件：</a>
         	<a href="#" onclick="javascript:opportunityListJs.openDateFilter()" class="easyui-linkbutton"  plain="true">时间</a>
    		
            <a href="#" id="employeeButton">所有人</a>
                <div id="mm3" class="menu-content" style="background:#f0f0f0;padding:10px;text-align:middle;width:200px;height:auto;display:none">
    				<div class="ftitle">按照所有人搜索</div>
    			    <div id="employee_list">
            		</div> 
    			</div>
    		<a href="#" id="itemButton">整形项目</a>
    		<div id="mm2" class="menu-content" style="background:#f0f0f0;padding:10px;text-align:middle;width:200px;height:auto;display:none">
    				<div class="ftitle">指定整形项目过滤</div>
    			    <div id="items_list" class="fitem">
            		</div> 
    		</div>
    		<a href="#" class="easyui-menubutton" data-options="menu:'#mm4'">当前阶段</a>
    		<div id="mm4" class="menu-content" style="background:#f0f0f0;padding:10px;text-align:middle;width:200px;height:auto">
    			    <div>
    			    	<p><a href="#" onclick="javascript:opportunityListJs.searchByStrategy('processing')">进行中</a></p>
    			    	<p><a href="#" onclick="javascript:opportunityListJs.searchByStrategy('success')">成功</a></p>
    			    	<p><a href="#" onclick="javascript:opportunityListJs.searchByStrategy('failed')">失败</a></p>
            		</div> 
    		</div>
        </div>
        <div id="opportunity_date" style="padding:3px">
        	从:<input id="opportunity_from" type="text"></input>
        	到:<input id="opportunity_to" type="text"></input>
        	<span class="toolbar_pic"><a href="javascript:void(0)" class="easyui-linkbutton" onclick="javascript:opportunityListJs.searchByTime()" >搜索</a></span>
        </div>
        <div id="searchFilter">
        </div>
    </div>
    <div id="OpportunityDetail"  class="easyui-dialog"  closed="true" style="width:900px;height:550px">
    </div>
    <script type="text/javascript">
(function(opportunityListJs, $, undefined ) {      
	var countPerPage = 5;
	var start = 0;
	var opportunity_strategy_search = '';
	var opportunity_customer_search = '';
	var opportunity_items_search = '';
	var opportunity_employee_search = '';
	
	var zxmr_items;
	var consultWay;
	opportunityListJs.opendiag = function(url,a){
		$('#CustomerAction').dialog("open");
		$('#CustomerAction').dialog('refresh',url);
		
	}
	opportunityListJs.clearSearch = function()
	{
		countPerPage = 5;
		start = 0;
		opportunity_strategy_search  = '';
		opportunity_customer_search = '';
		opportunity_items_search = '';
		opportunity_employee_search = '';
		$('#opportunity_from').datebox('setValue','');
		$('#opportunity_to').datebox('setValue','');
		refreshSearchFilter();
		refresh();
	}
	
	$('#list').datagrid({
		title:'商业机会列表',
		iconCls:'icon-opportunity',
		height:350,
		fitColumns:true,
		singleSelect:true,
		url:null,
		rownumbers:true,
	    columns:[[
					{title:'名称',field:'name',width:80},
	             	{title:'客户信息',field:'customerName',width:80,sortable:true},
	             	{title:'来源',field:'opp_from',width:80,sortable:true},
	             	{title:'当前阶段',field:'strategy',width:80,sortable:true},
	             	{title:'所有人',field:'owner',width:80,sortable:true},
	             	{title:'整形项目',field:'items',width:80,sortable:true},
	             	{title:'创建时间',field:'createTime',width:80,sortable:true},
	             ]],
		pagination:true,
        pageSize:countPerPage,
        pageList:[5,10,15,20,25],
		toolbar:'#tb',
	    onDblClickCell: openCustomer
	});

	$('#list').datagrid('getPager').pagination({   
	    displayMsg:'当前显示从 [{from}] 到 [{to}] 共[{total}]条记录',   
	    onSelectPage : function(pPageIndex, pPageSize) {   
	    	
	        var gridOpts = $('#list').datagrid('options');   
	        gridOpts.pageNumber = pPageIndex;   
	        gridOpts.pageSize = pPageSize;     

	        start = (pPageIndex -1)*pPageSize;
	        
	        postSearch();
	    },
	    onChangePageSize : function(pageSize){
	    	countPerPage = pageSize;
	    }
	}); 
	opportunityListJs.openDateFilter = function(){
		$('#opportunity_date').show();
	}

	opportunityListJs.init = function(){
		customerActionJs.setCustomerActionCallBack(opportunityListJs.CustomerActionCompelete);
		$('#opportunity_from').datebox();
		$('#opportunity_to').datebox();
		refresh();
	}
	
	opportunityListJs.CustomerActionCompelete = function(result){
		
		var value;
		if(typeof(result.payload)== 'undefined')
			value  = result;
		else
			value = eval('('+result.payload.value+')');
		
		
		$('#CustomerAction').dialog('close');
		$("input[name='customer']").attr("value",value.id);
		$("#customerName").html(value.name);
		
	}

	opportunityListJs.loadCAData = function(result){
    	var res = result.result;
        if(res == "SUCCESS")
        {
        	var payload = jQuery.parseJSON(result.payload.value);
        	$('#list').datagrid('loadData',payload);
        	
        }else if(res == "COMPONENT_ERROR")
        {
        	$.messager.alert("Error",result.payload.value);
        }else
        {
        	$.messager.alert("Error",result.result);
        }
        $('#list').datagrid("loaded");
	};
	opportunityListJs.loadItemsResult = function(result){
		
    	if(result.result == "SUCCESS")
    	{
    		zxmr_items = jQuery.parseJSON(result.payload.value);
    		var html = "";
    		$(zxmr_items).each(function(index){
    			html+='<p><a href="#" onclick="javascript:opportunityListJs.searchByItems(\''+zxmr_items[index].name+'\')"> '+zxmr_items[index].name+' </a></p>';
    		});
    		$('#items_list').html(html);
    		
    		
    	    $('#itemButton').menubutton({
    	        menu: '#mm2'
    	        });
    	}

	}
	function setItemsInput(){
		$('#items').combobox({
			url:null,
			valueField:'name',
			textField:'name',
			multiple:true,
			panelHeight:'auto',
			data:zxmr_items
		});
	}

	function openCustomer(){
        var row = $('#list').datagrid('getSelected');
        if (row){
    		$('#OpportunityDetail').dialog("open").dialog('setTitle','商业机会');
    		$('#OpportunityDetail').dialog('refresh','component/customer/viewOpportunity.jsp?id='+row.id);
        }else
        	{
        		$.messager.alert("Error","请选择一条记录");
        	}
	}
    var url;
    opportunityListJs.newCA = function(){
    	setItemsInput();
        $('#dlg').dialog('open').dialog('setTitle','增加商业机会');
        $('#fm').form('clear');
        $('#owner_div').hide();
        $('#customerName').html("");
        url = 'dispatch/customer/opportunity/add';
    }
    opportunityListJs.edit = function(){
        var row = $('#list').datagrid('getSelected');
        if (row){
    		$('#dlg').dialog("open").dialog('setTitle','修改商业机会');
    		$('#owner_div').show();
    		$('#fm').form('clear');
    		$('#fm').form('load',row);
    		// init items list
    		setItemsInput();
    		
    		// set customer name and id
    		$("#fm input[name='customer']").attr("value",row.customer);
    		$("#fm input[name='id']").attr("value",row.id);
    		$('#customerName').html(row.customerName);
    		
    		// set items
        	if(typeof(row.items)!='undefined' && row.items != "")
        	{
    			var array = row.items.split(",");

    	    	var i = new Array();
    	    	$(array).each(function(index){
    	    		i[index] = array[index];
    	    	});
    	    	
           		$('#items').combobox('setValues',i);
        	}
        	
    		// change url to edit
    		url = 'dispatch/customer/opportunity/update';
        }else
        	{
        		$.messager.alert("Error","请选择一条记录");
        	}
    }
    function refresh(){
    	$('#opportunity_date').hide();
    	$('#list').datagrid("loading");
    	workbenchJs.loadData("dispatch/customer/opportunity/all?start="+start+"&size="+countPerPage,opportunityListJs.loadCAData,"查询商业机会");
    	workbenchJs.loadData("dispatch/manage/item/all",opportunityListJs.loadItemsResult,"查询手术项目");
    	workbenchJs.loadData("dispatch/manage/employee/simpleAll",opportunityListJs.loadEmployeeResult,"查询用户列表");
    	workbenchJs.loadData("dispatch/manage/consultway/all",opportunityListJs.loadConsultWayResult,"查询咨询途径");
    	
    }
    
    opportunityListJs.loadConsultWayResult = function(result){
    	if(result.result == "SUCCESS")
    	{
    		consultWay = jQuery.parseJSON(result.payload.value);
    		$('#opp_from').combobox({
    			url:null,
    			valueField:'name',
    			textField:'name',
    			multiple:false,
    			panelHeight:'auto',
    			data:consultWay
    		});
    	}
    };
    opportunityListJs.loadEmployeeResult = function(result){
    	if(result.result == "SUCCESS")
    	{
    		var rows = jQuery.parseJSON(result.payload.value);
    		var html = "";
    		html+='<p><a href="#" onclick="javascript:opportunityListJs.searchByEmployee(\'*\')"> 全部</a></p>';
    		$(rows).each(function(index){
    			html+='<p><a href="#" onclick="javascript:opportunityListJs.searchByEmployee(\''+rows[index].name+'\')"> '+rows[index].name+' </a></p>';
    		});
    		$('#employee_list').html(html);
    		
    	    $('#employeeButton').menubutton({
    	        menu: '#mm3'
    	        });
    	}
    };
    opportunityListJs.save = function(){
    	
    	// first 
    	var json = {};
    	json.id = parseInt($("input[name='id']").val());
    	json.name = $("input[name='name']").val();
    	json.customer = parseInt($("input[name='customer']").val());
    	json.opp_from = $("input[name='opp_from']").val();
    	json.strategy = $("input[name='strategy']").val();
    	json.owner = $("input[name='owner']").val();
    	
    	var items = "";
    	var r = $('#items').combobox('getValues');
    	$(r).each(function(index){
    		if(items != "")items += ','+r[index];
    		else items = r[index];
    	});
    	json.items = items;
    	
    	
    	var str = $.toJSON(json);
    	str = 'parameters='+str;
    	if($('#fm').form('validate'))
    	{
    		$.ajax({
                type: "post",
                url: url,
                dataType:"json",
                data: str,
                cache: false,
                success: function (result) {
                	var res = result.result;
                    if(res == "SUCCESS")
                    {
                    	$('#dlg').dialog('close');
                    	refresh();
                    }else if(res == "COMPONENT_ERROR")
                    {
                    	$.messager.alert("Error",result.payload.value);
                    }else
                    {
                    	$.messager.alert("Error",result.result);
                    }
                    
                }
                
            });
    	}
		
    }
    
    opportunityListJs.searchByStrategy = function(strategy){
		opportunity_strategy_search = strategy;
		$('#list').datagrid("loading");
		refreshSearchFilter();
		start = 0;
		postSearch();
	}
    opportunityListJs.searchByItems = function(item){
		opportunity_items_search = item;
		$('#list').datagrid("loading");
		refreshSearchFilter();
		start = 0;
		postSearch();
	}
    opportunityListJs.searchByEmployee = function(employee){
		opportunity_employee_search = employee;
		$('#list').datagrid("loading");
		refreshSearchFilter();
		start = 0;
		postSearch();
	}

    opportunityListJs.searchByTime = function(){
		$('#list').datagrid("loading");
		refreshSearchFilter();
		start = 0;
		postSearch();
	}
	function getSearchFilter()
	{
		var html = "查询条件:";
		if(opportunity_strategy_search != "" && typeof(opportunity_strategy_search) != 'undefined')
			html += opportunity_strategy_search;
		
		if(opportunity_customer_search != "" && typeof(opportunity_customer_search) != 'undefined')
			html += " + "+opportunity_customer_search;
		if(opportunity_items_search != "" && typeof(opportunity_items_search) != 'undefined')
			html += " + "+opportunity_items_search;
		if(opportunity_employee_search != "" && typeof(opportunity_employee_search) != 'undefined')
			html += " + "+opportunity_employee_search;
		var opportunity_from = $('#opportunity_from').datebox('getValue');
		if(opportunity_from != "" && typeof(opportunity_from) != 'undefined')
			html += " + 时间从 "+opportunity_from;
		
		var opportunity_to = $('#opportunity_to').datebox('getValue');
		if(opportunity_to != "" && typeof(opportunity_to) != 'undefined')
			html += " + 时间到 "+opportunity_to;
		
		return html;
	}
	function refreshSearchFilter()
	{

		var html = getSearchFilter();
		html += ' <a href="javascript:void(0)" onclick="javascript:opportunityListJs.clearSearch()">清除</a>';
		$('#searchFilter').html(html);
		
	}
	function postSearch()
	{
		var str = "";
		var opportunity_from = $('#opportunity_from').datebox('getValue');
		if(typeof(opportunity_from) == 'undefined')
			opportunity_from = '';
		
		var opportunity_to = $('#opportunity_to').datebox('getValue');
		if(typeof(opportunity_to) == 'undefined')
			opportunity_to = '';
		
		str = appendURL(str,"strategy="+opportunity_strategy_search);
		str = appendURL(str,"customer="+opportunity_customer_search);
		str = appendURL(str,"items="+opportunity_items_search);
		str = appendURL(str,"employee="+opportunity_employee_search);
		str = appendURL(str,"from="+opportunity_from);
		str = appendURL(str,"to="+opportunity_to);
		str = appendURL(str,'start='+start+'&size='+countPerPage);
		$.ajax({
            type: "post",
            url: "dispatch/customer/opportunity/all",
            dataType:"json",
            data: str,
            cache: false,
            success: function (result) {
            	
            	opportunityListJs.loadCAData(result);
            }
		});
	}
	function appendURL(url,str1)
	{
		if(typeof(str1) == 'undefined')return url;
		if(url == "" || typeof(url) == 'undefined')url = str1;
		else url+="&"+str1;

		return url;
	}
}( window.opportunityListJs = window.opportunityListJs || {}, jQuery ));

    $(function(){
    	opportunityListJs.init();
    });  
    </script>
    <style type="text/css">
        #fm{
            margin:0;
            padding:10px 30px;
        }
        .ftitle{
            font-size:14px;
            font-weight:bold;
            padding:5px 0;
            margin-bottom:10px;
            border-bottom:1px solid #ccc;
        }
        .fitem{
            margin-bottom:5px;
        }
        .fitem label{
            display:inline-block;
            width:80px;
        }
        .toolbar_pic {
 			width: 70px;
  			height: 30px;
  			overflow: hidden;
  			display: inline-block;
  			vertical-align: top;
  			cursor: pointer;
  			opacity: 0.6;
  			filter: alpha(opacity=60);
		}
		.toolbar_text {
 			width: 70px;
 			height:100%;
  			overflow: hidden;
  			display: inline-block;
  			vertical-align: middle;
		}
    </style>